package com.raychi.demo.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.raychi.demo.dao.BuyerDao;
import com.raychi.demo.entity.Buyer;
import com.raychi.demo.util.JdbcUtil;

public class BuyerService {
	
	private BuyerDao buyerDao;
	
	public List<Buyer> getBuyers() throws ClassNotFoundException, SQLException{
		Connection conn = null;
		List<Buyer> list = null;
		try{
			conn = JdbcUtil.getConnection();
			list = buyerDao.getBuyers(conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
		return list;
	}
	
	public void delete(Buyer buyer){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			buyerDao.delete(buyer, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void save(Buyer buyer){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			buyerDao.save(buyer, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.rollback(conn);
		}finally{
			JdbcUtil.closeConnection(conn);
		}
	}
	
	public void update(Buyer buyer){
		Connection conn = null;
		try{
			conn = JdbcUtil.getConnection();
			buyerDao.update(buyer, conn);
			conn.commit();
		}catch(Exception e){
			JdbcUtil.closeConnection(conn);
		}finally{
			JdbcUtil.rollback(conn);
		}
	}
	public void setBuyerDao(BuyerDao buyerDao) {
		this.buyerDao = buyerDao;
	}
}
